#include "colorchoose.h"

ColorChoose::ColorChoose(QWidget *parent)
    : QMainWindow{parent}
{
    this -> setWindowTitle("颜色选择界面");
    this -> showFullScreen();
    this -> setWindowFlag(Qt::FramelessWindowHint);

    QPalette palette(QPalette::Window,QColorConstants::Svg::lightblue);
    this -> setPalette(palette);

    QWidget * colorMainWidget = new QWidget(this);
    setCentralWidget(colorMainWidget);


    layout = new QVBoxLayout();
    colorMainWidget -> setLayout(layout);

    confirm = new QPushButton("确认完成选择",this);
    confirm -> setStyleSheet("color:white;"
                           "background-color:lightblue;"
                           "padding:3px 6px;"
                           "font-size:30px;"
                           "border-radius:5px;");
    confirm -> setEnabled(false);
    confirm -> setCursor(Qt::CrossCursor);


    Close = new QPushButton("关闭",this);
    Close -> setStyleSheet("color:white;"
                         "background-color:lightblue;"
                         "padding:3px 6px;"
                         "font-size:30px;"
                         "border-radius:5px;");
    Close -> setEnabled(true);
    Close -> setCursor(Qt::PointingHandCursor);

    select = new QPushButton("选择",this);
    select -> setStyleSheet("color:white;"
                          "background-color:lightblue;"
                          "padding:3px 6px;"
                          "font-size:30px;"
                          "border-radius:5px;");
    select -> setEnabled(false);
    select -> setCursor(Qt::CrossCursor);

    back = new QPushButton("回退",this);
    back -> setStyleSheet("color:white;"
                        "background-color:lightblue;"
                        "padding:3px 6px;"
                        "font-size:30px;"
                        "border-radius:5px;");
    back -> setEnabled(false);
    back -> setCursor(Qt::CrossCursor);

    color = new QColor[totalPlayer + 1];
    color[0] = QColor(0,0,0);


    layout -> addWidget(select);
    layout -> addWidget(back);
    layout -> addWidget(confirm);
    layout -> addWidget(Close);

    //connect(this,SIGNAL(colorConfirmButtonClicked(bool)),w,SLOT(ifColorChosen(bool)));


    connect(Close,&QPushButton::clicked,this,&ColorChoose::onCloseButtonClicked);
    connect(confirm,&QPushButton::clicked,this,&ColorChoose::onConfirmButtonClicked);
    connect(select,&QPushButton::clicked,this,&ColorChoose::onSelectButtonClicked);
    connect(back,&QPushButton::clicked,this,&ColorChoose::onBackButtonClicked);
}
ColorChoose::~ColorChoose(){
    // for(int i = 0; i < totalPlayer + 1 ; i++){
    //     delete[] color[i];
    // }
    delete[] color;
    delete confirm;
    w = nullptr;
}
void ColorChoose::onConfirmButtonClicked(){
    //emit colorConfirmButtonClicked(true);
    // for(int i = 1 ; i <= totalPlayer ; i++ ){
    //     qDebug() << "QColor Chosed:" << color[i].name();
    // }
    emit chosedColorSiganl(color);
    w -> show();
    this -> currentchoose = 1;
    this -> close();
}
void ColorChoose::getTotalPlayer(int TotalPlayer){
    totalPlayer = TotalPlayer;
}
// void ColorChoose::ifConfirmAct(bool ret){
//     ifConfirm = ret;
// }
void ColorChoose::getMainWindow(MainWindow* W){
    w = W;
    // ifGet = true;
    connect(this,SIGNAL(chosedColorSiganl(QColor*)),w,SLOT(receivedColor(QColor*)));
    // connect(this,SIGNAL(ifBothConfirm(bool)),w,SLOT(ifConfirmAct(bool)));
    back -> setEnabled(true);
    back -> setCursor(Qt::PointingHandCursor);
    select -> setEnabled(true);
    select -> setCursor(Qt::PointingHandCursor);
    confirm -> setEnabled(true);
    confirm -> setCursor(Qt::PointingHandCursor);
}
// void ColorChoose::getColor(){
//     color[0] = QColor(0,0,0);
// }
void ColorChoose::onCloseButtonClicked(){
    emit closeSignal();
    this -> close();
}
void ColorChoose::onSelectButtonClicked(){
    ifSelect = true;
    if(currentchoose <= totalPlayer){
        select -> setEnabled(true);
        select -> setCursor(Qt::PointingHandCursor);
        color[currentchoose] = QColorDialog::getColor(QColor(currentchoose ,255 - currentchoose , 255 / 2), this ,QString("为玩家%1选择颜色").arg(currentchoose));
        currentchoose ++;
    }else{
        select -> setEnabled(false);
        select -> setCursor(Qt::CrossCursor);
        confirm -> setEnabled(true);
        confirm -> setCursor(Qt::PointingHandCursor);
    }
}
void ColorChoose::onBackButtonClicked(){
    if(currentchoose > 0){
        back -> setEnabled(true);
        back -> setCursor(Qt::PointingHandCursor);
        confirm -> setEnabled(false);
        confirm -> setCursor(Qt::CrossCursor);
        currentchoose --;
    }else{
        back -> setEnabled(false);
        back -> setCursor(Qt::CrossCursor);
        confirm -> setEnabled(false);
        confirm -> setCursor(Qt::CrossCursor);
    }
}
// void ColorChoose::ifConfirm(bool ret){
//     emit ifBothConfirm(ret and ifSelect);
// }
